package cn.wolfcode.crm.shiro;

import cn.wolfcode.crm.service.IMenuService;
import lombok.Setter;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

//修改登录逻辑用的
public class ExtendsFormAuthenticationFilter extends FormAuthenticationFilter {
    @Setter
    private IMenuService menuService;

    @Override
    public boolean onLoginSuccess(AuthenticationToken token,
                                     Subject subject, ServletRequest request, ServletResponse response)
            throws Exception {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        //-------------下面的别删掉了---------------------
       /* //当用户登录成功之后查询出其拥有的菜单集合,缓存到session中
        //系统中所有的菜单
        List<Menu> menus = menuService.queryRootMenu();
        //过滤不符合条件的菜单
        MenuUtil.filterMenu(menus);
        //将菜单设置到session中
        SecurityUtils.getSubject().getSession().setAttribute(MenuUtil.MENU_IN_SESSION,menus);*/
        //----------------上面的别删掉了------------------
        //登录成功后响应json回去进行判断
        resp.setCharacterEncoding("utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println("{\"success\":true,\"msg\":\"登录成功!\"}");
        writer.flush();
//        关闭流
        writer.close();
//        流程结束，响应浏览器
        return false;
    }
    @Override
    public boolean onLoginFailure(AuthenticationToken token,
                                     AuthenticationException e, ServletRequest request,
                                     ServletResponse response) {
        try {
            response.setCharacterEncoding("utf-8");
//            获取登录失败后的异常类字节码对象的简单名字
            PrintWriter writer = response.getWriter();
            String simpleName = e.getClass().getSimpleName();
            if("IncorrectCredentialsException".equals(simpleName))
            {
                writer.println("{\"success\":false,\"msg\":\"密码错误!\"}");
            }
            else if("UnknownAccountException".equals(simpleName))
            {
                writer.println("{\"success\":false,\"msg\":\"账号不存在!\"}");
            }
            else if("LockedAccountException".equals(simpleName))
            {
                writer.println("{\"success\":false,\"msg\":\"账号锁定!\"}");
            }
            else if("AuthenticationException".equals(simpleName))
            {
                writer.println("{\"success\":false,\"msg\":\"账号锁定!\"}");
            }
            else
            {
                writer.println("{\"success\":false,\"msg\":\"未知错误!\"}");
            }
            writer.flush();
    //        关闭流
            writer.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }

//        流程结束，响应浏览器
        return false;
    }

}
